package com.hq.fxh.No1_Infromation.Video.Activity;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.imageLoader.video.VideoItem;
import com.hq.fxh.imageLoader.video.VideoPicker;

import java.util.ArrayList;


public class VideoUpdateActivity extends BaseActivity implements View.OnClickListener {


    protected TextView tvUpdataVideoUpLoad;
    protected TextView tvUpdataVideoCancel;
    protected ImageView imageUpdataVideoIcon;
    protected EditText editUpdataVideoTitle;
    protected EditText editUpdataVideoContent;

    protected String videoPath;
    Bitmap bitmap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_update;
    }

    @Override
    protected void initView() {
        tvUpdataVideoUpLoad = (TextView) findViewById(R.id.tv_UpdataVideo_UpLoad);
        tvUpdataVideoUpLoad.setOnClickListener(VideoUpdateActivity.this);
        tvUpdataVideoCancel = (TextView) findViewById(R.id.tv_UpdataVideo_Cancel);
        tvUpdataVideoCancel.setOnClickListener(VideoUpdateActivity.this);
        imageUpdataVideoIcon = (ImageView) findViewById(R.id.image_UpdataVideo_Icon);
        editUpdataVideoTitle = (EditText) findViewById(R.id.edit_UpdataVideo_title);
        editUpdataVideoContent = (EditText) findViewById(R.id.edit_UpdataVideo_Content);
    }

    @Override
    protected void initData() {

        try {
            VideoPicker videoPicker = VideoPicker.getInstance();
            ArrayList<VideoItem> selectedVideos = videoPicker.getSelectedVideos();
            if (selectedVideos == null || selectedVideos.size() == 0) {
                Intent intent = getIntent();
                Bundle extras = intent.getExtras();
                videoPath = extras.getString("path");
            } else {
                VideoItem videoItem = selectedVideos.get(0);
                videoPath = videoItem.path;
            }


        } catch (Exception e) {
        } finally {
            if (videoPath == null) {
                LogUtils.startToash(this, "视频选择失败！");
            }
            LogUtils.e(videoPath + "收到的视频地址");
            MediaMetadataRetriever media = new MediaMetadataRetriever();
            media.setDataSource(videoPath);
            bitmap = media.getFrameAtTime();
            imageUpdataVideoIcon.setImageBitmap(bitmap);
        }

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_UpdataVideo_UpLoad) {

            upVideo();


        } else if (view.getId() == R.id.tv_UpdataVideo_Cancel) {
            finish();
        }
    }

    public void upVideo() {
        LogUtils.startToash(this, "上传视频开始");
        // UpLoadVideoProgressUtils.up(VideoUpdateActivity.this);
        show3();
        UpLoadVideoProgressUtils.upLoadCoverImg(VideoUpdateActivity.this, "title", bitmap, builder, manager);
    }

    private static final int NO_3 = 0x3;
    NotificationCompat.Builder builder;
    NotificationManager manager;

    public void show3() {
        builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.ldd_logo);
        builder.setContentTitle("视频上传");
        builder.setContentText("正在上传");
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NO_3, builder.build());
        builder.setProgress(100, 0, false);
     /*   new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    builder.setProgress(100, i, false);
                    manager.notify(NO_3, builder.build());
                    //下载进度提示
                    builder.setContentText("下载" + i + "%");
                    try {
                        Thread.sleep(50);//演示休眠50毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //下载完成后更改标题以及提示信息
                builder.setContentTitle("开始安装");
                builder.setContentText("安装中...");
                //设置进度为不确定，用于模拟安装
                builder.setProgress(0, 0, true);
                manager.notify(NO_3, builder.build());
                manager.cancel(NO_3);//设置关闭通知栏
            }

        }).start();
*/
    }

}
